package com.cyy.defstep;

import com.cyy.service.AppiumService;
import com.cyy.utils.Common;
import io.cucumber.java.Scenario;
import io.cucumber.java.zh_cn.当;
import io.cucumber.java.zh_cn.那么;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.springframework.beans.factory.annotation.Autowired;
import java.net.MalformedURLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;

@Slf4j
public class AppiumDefstep extends AbstractDefstep {
    @Autowired
    AppiumService appiumService;
    @Autowired
    Common common;

    String series;

    List<WebElement> els = new ArrayList<>();

//    @BeforeStep("@app")
    public void before_step_app(Scenario scenario) throws ParseException {
        String name = common.today("yyyyMMddhhmmss") +".png";
        String remotepath = null;
        try {
            String path = common.get_project_path()+"/temp/" + name;
            Map<String, Object> screen = appiumService.screen(path, series);
            if (screen == null) return;
            remotepath = screen.get("remotepath").toString();
            scenario.attach((byte[]) screen.get("byte"),"image/png","步骤截图");
        }catch (Exception e) {
            log.error(e.getMessage());
        }finally {
            if (remotepath!=null) appiumService.rm_file(remotepath,series);
        }
    }

    @当("^唤醒手机$")
    public void wake_app() {
        appiumService.wake(series);
    }

    @当("^安装驱动,测试环境为ip=(.*?),port=(.*?),为(.*?)系统$")
    public void install_driver(String ip, int port,String platformName) throws MalformedURLException {
        this.series = ip+":"+port;
        appiumService.installDriver(platformName, series);
    }

    @当("^安装app$")
    public void installApp() {
        appiumService.installApk(series);
    }

    @那么("^退出小程序$")
    public void quit() {
        if (appiumService.is_repload()) {
            appiumService.waituntil(series,2000).close(series)
                    .uninstallApk(series);
        } else {
            appiumService.waituntil(series,1000).quit(series);
        }
    }

    @那么("^卸载小程序:(.*?)$")
    public void uninstall(String apkpath) {
        String path = common.get_project_path() + "/" + apkpath;
        appiumService.uninstallApk(series);
    }

    @那么("^向左滑屏(.*?)次$")
    public void swipe_left(int num) {
        for (int i = 0; i < num; i++) {
            appiumService.swipe_left(series);
        }
    }

    @那么("^向右滑屏(.*?)次$")
    public void swipe_right(int num) {
        for (int i = 0; i < num; i++) {
            appiumService.swipe_right(series);
        }
    }

    @那么("^向上滑屏(.*?)次$")
    public void swipe_top(int num) {
        for (int i = 0; i < num; i++) {
            appiumService.swipe_top(series);
        }
    }

    @那么("^向下滑屏(.*?)次$")
    public void swipe_down(int num) {
        for (int i = 0; i < num; i++) {
            appiumService.swipe_down(series);
        }
    }

    @当("^查找元素,元素id=(.*?)$")
    public void find_els(String id) {
        els = appiumService.find_elemets_by(series,By.id(id));
    }

    @当("^查找元素,元素xpath=(.*?)$")
    public void find_els_xapth(String xpath) {
        els = appiumService.find_elemets_by(series,By.xpath(xpath));
    }

    @当("^设置动态查找元素的超时时长(.*?)mills$")
    public void set_timeout(int tm) {
        appiumService.timeout(tm);
    }

    @当("^等待(.*?)毫秒$")
    public void wait(int mills) {
        common.sleep(mills);
    }

    @那么("^点击元素(.*?),对象id=(.*?)$")
    public void click_element(String name, String id) {
        log.info("点击元素" + name + "》元素定位id=" + id);
        appiumService.click(series,By.id(id));
    }

    @那么("^点击(.*?)次元素(.*?),元素xpath=(.*?)$")
    public void click_element_by_xpath(int num, String name, String xpath) {
        log.info("点击元素" + name + "》元素定位xpath=" + xpath);
        for (int i = 0; i < num; i++) {
            appiumService.click(series,By.xpath(xpath));
        }
    }

    @那么("^点击元素(.*?),元素xpath=(.*?)$")
    public void click_element_by_xpath(String name, String xpath) {
        log.info("点击元素" + name + "》元素定位xpath=" + xpath);
        appiumService.click(series,By.xpath(xpath));
    }

    @那么("^向元素(.*)id\\((.*?)\\)输入字符\\((.*?)\\)$")
    public void send_keys_by_id(String name, String id, String input) {
        log.info("点击元素" + name + "》元素定位id=" + id);
        appiumService.input(series,By.id(id), input);
    }

    @那么("^向元素(.*?)xpath\\((.*?)\\)输入字符\\((.*?)\\)$")
    public void send_keys_by_xpath(String name, String xpath, String input) {
        log.info("点击元素" + name + "》元素定位xpath=" + xpath);
        appiumService.input(series,By.xpath(xpath), input);
    }

    @当("手机处于熄屏状态,唤醒手机")
    public void awake() {
        appiumService.wake(series);
    }

    @那么("^检查点:存在对象(.*?)id=(.*?)$")
    public void check_element_exist_byid(String name, String id) {
        WebElement elemet = appiumService.find_element_by(series,By.id(id));
        log.info("检查元素" + name + "是否存在 》元素定位id=" + id + "》结果:" + (elemet == null));
        assertThat(elemet, notNullValue());
    }

    @那么("^检查点:存在对象(.*?)xpath((.*?))$")
    public void check_element_exist_byxpath(String name, String xpath) {
        WebElement elemet = appiumService.find_element_by(series,By.xpath(xpath));
        log.info("检查元素" + name + "是否存在 》元素定位xpath=" + xpath + "》结果:" + (elemet == null));
        assertThat(elemet, notNullValue());
    }

    @那么("^检查点:对象(.*?)id=(.*?)的text等于(.*?)$")
    public void check_element_text_equal(String name, String id, String expected) {
        WebElement elemet = appiumService.find_element_by(series,By.id(id));
        assertThat("元素为空", elemet, notNullValue());
        assertThat(elemet.getText(), equalTo(expected));
        log.info("检查元素" + name + "属性text 》元素定位id=" + id + "》结果:" + (elemet.getText()));
    }


}
